package com.hzqy.web.ac;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sun.management.OperatingSystemMXBean;

@Controller
public class GetInfo {
	
	@RequestMapping("/getInfo/getRam")
	@ResponseBody
	public Map<String, Long> getRam() {
		Map<String, Long> ramMap = new HashMap<String, Long>();
		ramMap.put("max", Runtime.getRuntime().maxMemory());
		ramMap.put("used", Runtime.getRuntime().totalMemory());
		ramMap.put("free", Runtime.getRuntime().freeMemory());
		return ramMap;
	}

	@RequestMapping("/getInfo/getDisk")
	@ResponseBody
	public Object getDisk() {
		List<Map<String, Object>> useageList = new ArrayList<Map<String, Object>>();
		File[] roots = File.listRoots();// 获取磁盘分区列表
		for (File file : roots) {
			long totalSpace = file.getTotalSpace();
			long freeSpace = file.getFreeSpace();
			long usableSpace = file.getUsableSpace();
			if (totalSpace > 0) {
				Map<String, Object> useageMap = new HashMap<String, Object>();
				useageMap.put("total", totalSpace);
				useageMap.put("used", totalSpace - freeSpace);
				useageMap.put("free", freeSpace);
				useageMap.put("usable", usableSpace);
				useageMap.put("pf", file.getPath());
				useageList.add(useageMap);
			}
		}
		return useageList;
	}

	@RequestMapping("getInfo/gethardware")
	@ResponseBody
	public Object getInfos() {
		List<Map<String, Object>> hardwareInfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> hardware = new HashMap<String, Object>();
		OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
		hardware.put("systemName", osmb.getName() + " " + osmb.getArch());
		hardware.put("javaVersion", System.getProperty("java.version"));
		hardware.put("userName", System.getProperty("user.name"));
		hardware.put("systemVersion", System.getProperty("os.version"));
		hardware.put("isLinux", System.getProperty("os.name").indexOf("Windows") < 0);
		hardware.put("systemTotalMemory", osmb.getTotalPhysicalMemorySize());
		hardwareInfo.add(hardware);
		return hardware;
	}


	@RequestMapping("getInfo/getJavaRamSysInfo")
	@ResponseBody
	public Object getJaASi() {
		Map<Object, Object> infoMap = new HashMap<Object, Object>();
		Runtime rtm = Runtime.getRuntime();
		infoMap.put("javaTolRam", rtm.totalMemory());
		infoMap.put("javaMaxRam", rtm.maxMemory());
		infoMap.put("free", rtm.freeMemory());
		// infoMap.put("SystemInfo", SigarLoader.IS_LINUX);
		return infoMap;
	}

	
}
